scirius
scirius介绍
scirius为suricata提供了一个web,用来管理规则以及处理告警,我们分析一下他是如何实现的。
先分析主目录的views.py
定义了重定向到kinaba、evebox、moloch的路由,没什么关键函数。我们主要关心的是hunt和manage。
rules
看一下rules目录下的views.py
访问manage页面,主要实现了两个功能
- suricata状态监控,配置
- rule管理
我们看一下是如何实现的?
抓包发现前端会不断请求以下路径
用来获取suricata状态的info路径对应函数如下
rules/views.py1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71PROBE = __import__(settings.RULESET_MIDDLEWARE)
def info(request):
data = {'status': 'green'}
if request.GET.__contains__('query'):
info = PROBE.common.Info()
query = request.GET.get('query', 'status')
if query == 'status':
data = {'running': info.status()}
elif query == 'disk':
data = info.disk()
elif query == 'memory':
data = info.memory()
elif query == 'used_memory':
data = info.used_memory()
elif query == 'cpu':
data = info.cpu()
return JsonResponse(data, safe=False)
#在settings.py里可以看到RULESET_MIDDLEWARE='suricata',所以PROBE相当于__import__(settings.suricata),动态加载了suricata。而suricata/common.py如下
import psutil
from rest_framework import serializers
from django.conf import settings
if settings.SURICATA_UNIX_SOCKET:
try:
import suricatasc
except:
settings.SURICATA_UNIX_SOCKET = None
class Info():
def status(self):
suri_running = 'danger'
# 判断suricata是否运行则有两种方式,如果配置了suricata_unix_socket则调用suricatasc并发送uptime来判断。
if settings.SURICATA_UNIX_SOCKET:
sc = suricatasc.SuricataSC(settings.SURICATA_UNIX_SOCKET)
try:
sc.connect()
except:
return 'danger'
res = sc.send_command('uptime', None)
if res['return'] == 'OK':
suri_running = 'success'
sc.close()
# 如果没有配置SURICATA_UNIX_SOCKET则调用psutil.process_iter()来判断是否存在Suricata-Main来实现
else:
for proc in psutil.process_iter():
try:
pinfo = proc.as_dict(attrs=['name'])
except psutil.NoSuchProcess:
pass
else:
if pinfo['name'] == 'Suricata-Main':
suri_running = 'success'
break
return suri_running
#可以看到该函数调用psutil获取cpu/disk/mem状态
def disk(self):
return psutil.disk_usage('/')
def memory(self):
return psutil.virtual_memory()
def used_memory(self):
mem = psutil.virtual_memory()
return round(mem.used * 100. / mem.total, 1)
def cpu(self):
return psutil.cpu_percent(interval=0.2)
suricatasc可以实时和suricata进程进行交互
接下来看一下核心功能之一-规则的管理,先大概了解一下功能
- /rules/source可以看到规则源
- /rules/source/add_public可以增加公共规则源
- /rules/source/add可以编辑自定义规则源
- /rules/ruleset/add可以新增规则集
- /rules/ruleset/n/update可以更新规则集
- /rules/ruleset/n/copy可以复制规则集
- /rules/ruleset/n/delete可以删除规则集
- /rules/ruleset/n/edit可以编辑规则集
- /rules/ruleset/1/edit?mode=sources可以启用/禁用source
- /rules/ruleset/1/edit?mode=categories可以启用/禁用categories
- /rules/ruleset/1/addsupprule可以禁用某条rule
- /rules/ruleset/1/edit?mode=rules可以恢复某条被禁用的rule
- /rules/category/n/可以查看category
- /rules/category/n/enable可以启用category
- /rules/category/n/disable可以禁用category
- /rules/category/n/transform
- /rules/rule/pk/n/可以查看rule的定义
- /rules/rule/pk/n/disable可以禁用rule
- /rules/rule/pk/n/enable可以启用rule
- /rules/rule/n/edit
- /rules/rule/n/disable
- /rules/rule/n/enable
- /rules/rule/n/threshold?action=threshold为规则增加触发的阀值
- /rules/rule/n/threshold?action=suppress过滤某ip触发改规则
页面如下
/rules/ruleset 列出ruleset规则集
/rules/ruleset页面代码如下
1 | # Create your views here. |
页面如下
/rules/source 列出规则源
/rules/source页面代码如下
1 | def sources(request): |
最后获取了Source.objects.all()
页面如下
/rules/source/add_public增加公共规则源
/rules/source/add_public页面代码及分析如下
1 | def add_public_source(request): |
add_public_source.html页面代码及分析如下
1 | <!-- 获取source.yaml里面的source和参数 --> |
import_and_add_source.html1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183<script>
$( 'document' ).ready(function() {
<!-- 如果update为true则调用update_activate_source -->
{% if update %}
{% if not rulesets %}
update_activate_source({{ source.pk }}, null);
{% else %}
update_activate_source({{ source.pk }}, [ {{ rulesets|join:"," }} ], [ {{ ruleset_list|safeseq|join:"," }} ])
{% endif %}
window.addEventListener("beforeunload", function (e) {
if (!warn_on_exit) {
return;
}
var confirmationMessage = "Warning, leaving page will interrupt source addition mechanism.";
e.returnValue = confirmationMessage; // Gecko, Trident, Chrome 34+
return confirmationMessage; // Gecko, WebKit, Chrome <34
});
{% endif %}
});
function activate_ruleset_from(src_pk, rulesets, ruleset_list, r_length)
{
if (rulesets.length == 0) {
$('#source_progress').width("100%");
$('#source_progress').addClass("progress-bar-success");
$('#source_progress').removeClass("progress-bar-danger");
$('#source_progress').text("Source fully activated.");
$('#init_details').append("<p><a href='{{ source.get_absolute_url }}'>See details of {{ source.name }} source.</a></p>");
warn_on_exit = false;
return;
}
ri = rulesets.pop()
ruleset = ruleset_list.pop()
warn_on_exit = true;
var tgturl = "/rules/source/" + src_pk + "/activate/" + ri;
$.ajax({
url: tgturl,
type: 'POST',
success: function(data) {
if (data == true) {
var progress = 80 + (r_length - rulesets.length) * 20 / rulesets.length;
$('#source_progress').width(progress + "%");
$('#source_progress').text("Source activated in " + ruleset + ".");
$("#init_details").append('<p class="text-success"> <span class="glyphicon glyphicon-ok"></span> Source activated in "' + ruleset + '"</p>');
activate_ruleset_from(src_pk, rulesets, ruleset_list, r_length);
} else {
$('#source_progress').addClass("progress-bar-danger");
$('#source_progress').text("Could not activate source for '" + ruleset + "'.");
$('#init_details').append("<p class='text-danger'> <span class='glyphicon glyphicon-remove'></span> Could not activate source for '" + ruleset + "'.</p>");
warn_on_exit = false;
}
},
error: function(data) {
$('#source_progress').addClass("progress-bar-danger");
$('#source_progress').text("Unable activate source in '" + ruleset + "'.");
var error_actions = '<a href="{{ source.get_absolute_url }}delete"><button class="btn btn-primary" type="submit"><span class="glyphicon glyphicon-trash"> Delete source</span></button></a>'
error_actions += ' <a href="{{ source.get_absolute_url }}"><button class="btn btn-warning" id="continue" type="submit"><span class="glyphicon glyphicon-ok"> Ignore errors and continue</span></button></a>';
$("#init_details").append('<div id="error_actions">' + error_actions + '<div>');
warn_on_exit = false;
}
});
}
function update_activate_source(src_pk, rulesets, ruleset_list)
{
var tgturl = "/rules/source/" + src_pk + "/update";
$('#source_progress').text("Updating source.");
<!-- 通过ajax访问"/rules/source/" + src_pk + "/update" -->
$.ajax({
type:"POST",
url: tgturl,
<!-- 如果后端返回的data['status']为true -->
success: function(data) {
if (data['status'] == true) {
$("#init_details").append('<p class="text-success"> <span class="glyphicon glyphicon-ok"></span></span> Source updated</p>');
$('#source_progress').width("70%");
<!-- 如果成功获取到规则文件,会调用test_source去判断是否可以正常加载且和其他规则匹配 -->
test_source(src_pk, rulesets, ruleset_list);
<!-- 否则定义error_action -->
} else {
$('#source_progress').addClass("progress-bar-danger");
$('#source_progress').text("Could not test source.");
<!-- 把data['error']放到text-danger标签里 -->
$("#init_details").append('<p class="text-danger"> <span class="glyphicon glyphicon-remove"></span> Error during source update: ' + data['errors'] + '</p>');
var error_actions = '<a href="{{ source.get_absolute_url }}delete"><button class="btn btn-primary" type="submit"><span class="glyphicon glyphicon-trash"> Delete source</span></button></a>'
error_actions += ' <a href="{{ source.get_absolute_url }}"><button class="btn btn-warning" id="continue" type="submit"><span class="glyphicon glyphicon-ok"> Ignore errors and continue</span></button></a>';
$("#init_details").append('<div id="error_actions">' + error_actions + '<div>');
warn_on_exit = false;
}
},
error: function(data) {
$('#source_progress').addClass("progress-bar-danger");
$('#source_progress').text("Unable to update source.");
var err_str = 'Error during source update';
if (data.statusText && data.statusText != 'error') {
err_str += ' (' + data.statusText + ')';
}
$("#init_details").append('<p class="text-danger"> <span class="glyphicon glyphicon-remove"> ' + err_str + '</span> </p>');
var error_actions = '<a href="{{ source.get_absolute_url }}delete"><button class="btn btn-primary" type="submit"><span class="glyphicon glyphicon-trash"> Delete source</span></button></a>'
error_actions += ' <a href="{{ source.get_absolute_url }}"><button class="btn btn-warning" id="continue" type="submit"><span class="glyphicon glyphicon-ok"> Ignore errors and continue</span></button></a>';
$("#init_details").append('<div id="error_actions">' + error_actions + '<div>');
warn_on_exit = false;
},
timeout: 240 * 1000
});
}
function test_source(src_pk, rulesets, ruleset_list)
{
var tgturl = "/rules/source/" + src_pk + "/test";
$('#source_progress').text("Testing source.");
$.ajax({
url: tgturl,
success: function(data) {
if (data['status'] == true) {
$("#init_details").append('<p class="text-success"> <span class="glyphicon glyphicon-ok"></span></span> Source is valid</p>');
$('#source_progress').width("80%");
if (! rulesets) {
rulesets = []
}
if ('warnings' in data && data['warnings'][0] != undefined) {
var warning_content = "";
for (i = 0; i < data['warnings'].length; i++) {
warning_content += '<li><span class="text-warning">' + data['warnings'][i]['message'] + '</span></li>';
}
$("#init_details").append('<p class="text-warning"> <span class="glyphicon glyphicon-ok"></span> Source test warnings: <ul>' + warning_content + '</ul></p>');
}
activate_ruleset_from(src_pk, rulesets, ruleset_list, rulesets.length)
} else {
$('#source_progress').addClass("progress-bar-danger");
$('#source_progress').text("Source has errors.");
var error_content = "";
if (data['errors'][0] != undefined) {
for (i = 0; i < data['errors'].length; i++) {
error_content += '<li><span class="text-danger"><strong>' + data['errors'][i]['error'] + '</strong></span>: <span>' + data['errors'][i]['message'] + '</span></li>';
}
} else {
if (data['errors']['message'] != undefined) {
error_content = data['errors']['message'];
} else {
error_content = "Unknown error";
}
}
$("#init_details").append('<p class="text-danger"> <span class="glyphicon glyphicon-remove"></span> Source test failure: <ul>' + error_content + '</ul></p>');
if ('warnings' in data && data['warnings'][0] != undefined) {
var warning_content = "";
for (i = 0; i < data['warnings'].length; i++) {
warning_content += '<li><span class="text-warning">' + data['warnings'][i]['message'] + '</span></li>';
}
$("#init_details").append('<p class="text-warning"> <span class="glyphicon glyphicon-ok"></span> Source test warnings: <ul>' + warning_content + '</ul></p>');
}
var error_actions = '<a href="{{ source.get_absolute_url }}delete"><button class="btn btn-primary" type="submit"><span class="glyphicon glyphicon-trash"> Delete source</span></button></a>'
error_actions += ' <button class="btn btn-warning" id="continue" type="submit"> <span class="glyphicon glyphicon-ok"> Ignore errors and continue</span></button>';
$("#init_details").append('<div id="error_actions">' + error_actions + '</div>');
warn_on_exit = false;
$("#continue").click( function(event) {
$("#error_actions").slideUp();
if (! rulesets) {
rulesets = []
}
activate_ruleset_from(src_pk, rulesets, ruleset_list, rulesets.length)
});
}
},
error: function(data) {
$('#source_progress').addClass("progress-bar-danger");
$('#source_progress').text("Unable to test source.");
$("#init_details").append('<p class="text-danger"> <span class="glyphicon glyphicon-remove"></span> Error during source testing : ' + data.statusText + '</p>');
var error_actions = '<a href="{{ source.get_absolute_url }}delete"><button class="btn btn-primary" type="submit"><span class="glyphicon glyphicon-trash"> Delete source</span></button></a>'
error_actions += ' <a href="{{ source.get_absolute_url }}"><button class="btn btn-warning" id="continue" type="submit"><span class="glyphicon glyphicon-ok"> Ignore errors and continue</span></button></a>';
$("#init_details").append('<div id="error_actions">' + error_actions + '<div>');
warn_on_exit = false;
},
timeout: 1200 * 1000
});
}
</script>